package view;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.FileInputStream;

import javax.swing.JPanel;

/**
 * Laat de image zien in de banner.
 * @author Marvin
 * @author Yasper
 *
 */
@SuppressWarnings("serial")
public class Background extends JPanel {
	private Image bgImage;
	private static int BEGINHEIGHT = 700;
	private static int BEGINWIDTH  = 640;

	public Background(String imageFile){
		bgImage = getFileImage(imageFile);
		repaint();
	}
	
	/**
	 * Tekent de achtergrond.
	 * @author Yasper, Marvin
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2   = (Graphics2D) g;
		Double mbreedte = new Double(getWidth()) / BEGINWIDTH;
		Double mhoogte  = new Double(getHeight()) / BEGINHEIGHT;

		g2.scale(mbreedte, mhoogte);
		if (bgImage != null) {
			g.drawImage(bgImage, 0, 0, this);
		}
	}

	/**
	 * Haalt de achtergrond op.
	 * @author Yasper
	 * @return Image van opgehaald plaatje.
	 */
	private Image getFileImage(String filePath) {
		try {
			FileInputStream in = new FileInputStream(filePath);
			byte[] b           = new byte[in.available()];

			in.read(b);
			in.close();

			return Toolkit.getDefaultToolkit().createImage(b);
		} catch (Exception ex) {}
		return null;
	}

}
